// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
// import jQuery from 'jquery'
import MintUi, { DatetimePicker, Picker, Loadmore, Spinner, Indicator, Swipe, SwipeItem } from 'mint-ui'
import App from './App'
import router from './router'
// import {TENANT_TOKEN} from './assets/js/config'
// import LocalCache from './assets/js/cache'
import request from './assets/js/request'
import 'mint-ui/lib/style.css'
import './assets/css/reset.css'
import './assets/css/public.css'
import './assets/css/searchStyle.css'
import './assets/css/formStyle.css'

Vue.config.productionTip = false
Vue.use(MintUi, DatetimePicker.name, DatetimePicker, Picker.name, Picker, Loadmore.name, Loadmore, Spinner.name, Spinner, Indicator)
Vue.use(Swipe.name, Swipe, SwipeItem.name, SwipeItem)
import Vee, { Validator } from 'vee-validate'
import zhCN from 'vee-validate/dist/locale/zh_CN'

Validator.extend('mobile', {
  messages: {
    zh_CN: field => {
      return '请输入正确的手机号码'
    }
  },
  validate: value => {
    return value.length === 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value)
  }
})
Validator.extend('idCard', {
  getMessage (field, args) {
    return '请输入正确的身份证'
  },
  validate (cardNo, args) {
    if (!cardNo || (cardNo.length !== 15 && cardNo.length !== 18)) {
      return false
    }
    if (cardNo.length === 15) {
      let year = cardNo.substring(6, 8)
      let month = cardNo.substring(8, 10)
      let day = cardNo.substring(10, 12)
      let birthday = new Date(year, parseFloat(month) - 1, parseFloat(day))
      // 对于老身份证中的年龄则不需考虑千年虫问题而使用getYear()方法
      if (birthday.getYear() !== parseFloat(year) || birthday.getMonth() !== parseFloat(month) - 1 || birthday.getDate() !== parseFloat(day)) {
        return false
      } else {
        return true
      }
    }
    if (cardNo.length === 18) {
      let year = cardNo.substring(6, 10)
      let month = cardNo.substring(10, 12)
      let day = cardNo.substring(12, 14)
      let birthday = new Date(year, parseFloat(month) - 1, parseFloat(day))
      // 这里用getFullYear()获取年份，避免千年虫问题
      if (birthday.getFullYear() !== parseFloat(year) || birthday.getMonth() !== parseFloat(month) - 1 || birthday.getDate() !== parseFloat(day)) {
        return false
      }
      var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1]// 加权因子
      var Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]// 身份证验证位值.10代表X

      // 验证校验位
      var sum = 0 // 声明加权求和变量
      var _cardNo = cardNo.split('')

      if (_cardNo[17].toLowerCase() === 'x') {
        _cardNo[17] = 10// 将最后位为x的验证码替换为10方便后续操作
      }
      for (let i = 0; i < 17; i++) {
        sum += Wi[i] * _cardNo[i]// 加权求和
      }
      let i = sum % 11// 得到验证码所位置

      if (_cardNo[17] !== Y[i] + '') {
        return false
      }
      return true
    }
  }
})
console.log(request)
const veeConfig = {
  errorBagName: 'errors',
  delay: 0,
  message: null,
  strict: true,
  locale: 'zh_CN'
}
Validator.addLocale(zhCN)
Vue.use(Vee, veeConfig)

// const $ = jQuery
/* eslint-disable no-new */
Vue.filter('date', function (d) {
  try {
    if (d instanceof Date || new Date(d) instanceof Date) {
      if (typeof d === 'string') {
        d = new Date(d)
      }
      let year = d.getFullYear()
      let month = d.getMonth() + 1
      month = month < 10 ? ('0' + month) : month
      let date = d.getDate()
      date = date < 10 ? ('0' + date) : date
      return year + '-' + month + '-' + date
    } else {
      return d
    }
  } catch (e) {
    return d
  }
})
window.myVue = new Vue({
  el: '#app',
  router,
  template: '<App/>',
  created () {
    Vue.prototype.post = request.post(this)
    Vue.prototype.get = request.get(this)
  },
  methods: {},
  components: {App}
})
